package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.AllocationBean;
import com.wnhz.work.yz.bean.SingleItemBean;

import java.util.List;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class MianListAdapter extends BaseAdapter {

    private List<AllocationBean> data2;
    private LayoutInflater layoutInflater;
    private Context context;

    public MianListAdapter(Context context, List<AllocationBean> data2) {
        this.context = context;
        this.data2 = data2;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data2.size();
    }

    @Override
    public Object getItem(int position) {
        return data2.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ZuJian zuJian = null;
        if (convertView == null) {
            zuJian = new ZuJian();
            // 获取组件布局
            convertView = layoutInflater.inflate(R.layout.subject_content_item, null);

            zuJian.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            zuJian.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            zuJian.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            zuJian.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            zuJian.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            zuJian.ll_item = (LinearLayout) convertView.findViewById(R.id.ll_item);

            // 这里要注意，是使用的tag来存储数据的。
            convertView.setTag(zuJian);
        } else {
            zuJian = (ZuJian) convertView.getTag();
        }

        Glide.with(context).load(data2.get(position).getGoods_pic()).error(R.mipmap.img_chanpinfenlei1).crossFade().into(zuJian.iv_img);
        zuJian.tv_name.setText(data2.get(position).getGoods_name());
        zuJian.tv_type.setText(data2.get(position).getSku_info());
        zuJian.tv_price.setText("");
        zuJian.tv_number.setText("x" + data2.get(position).getGoods_nums());
        if(  zuJian.tv_type.getText().toString().length()>15){
          zuJian.iv_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast    toast = Toast.makeText(context,
                            data2.get(position).getSku_info(), Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();

                }
            });
  zuJian.tv_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast    toast = Toast.makeText(context,
                            data2.get(position).getSku_info(), Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();

                }
            });



        }


        return convertView;
    }

    public class ZuJian {
        ImageView iv_img;
        TextView tv_name;
        TextView tv_type;
        TextView tv_price;
        LinearLayout ll_item;
        TextView tv_number;
    }
}




